import ssl
import logging
import pytest
from websocket import create_connection
from config import datas


class TestWebTerminal():
    ssl_options = {
        "cert_reqs": ssl.CERT_NONE,  # 禁用证书验证
        "check_hostname": False,  # 禁用主机名检查
        "ssl_version": ssl.PROTOCOL_TLS_CLIENT  # 指定协议版本
    }

    @pytest.mark.normal
    @pytest.mark.webterminal
    def test_terminal(self, get_cookie):
        cookie = get_cookie['value']
        try:
            ws = create_connection('wss://' + datas.IP + ':31616/clusters/host/rest/webterminal/v1/user/admin/terminal',
                                   header={"Cookie": cookie},
                                   sslopt=TestWebTerminal.ssl_options)
            logging.info(ws.recv())
            ws.close()
        except ssl.SSLCertVerificationError as e:
            logging.info(f"证书验证失败: {e.verify_message}")
            logging.info(f"错误代码: {e.verify_code}")
